const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const myUnit = require('../../express/bin/unit/myUnit.js');
const upload_code_control = require('./upload_code_control.js');
const upload_text_control = require('./upload_text_control.js');
/*
* require的路径是基于file_upload_express.js文件所在路径
* */
let file_upload_express =function () {
    let app = express();

    let dirConfig = {
        register_rootDir:''
    };

    /*
    * 这里的路径是基于expressManger.js所在路径
    * */
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(express.static(path.resolve('../TaskAndModule/dist_prod')));

    let port = ****
    app.listen(port, function () {
        let host = 'localhost';
        console.log('Example app listening at http://%s:%s', host, port);
    });

    app.get('/',function (req,res) {
        console.log(req.url);
        res.sendFile(path.resolve('../TaskAndModule/dist_prod/register.html'));
    });

    app.post('/register', function (req, res) {
        // let file_upload = 'file_upload';
        // app.use(express.static(path.resolve('../dist/file_upload')));
        // let name = req.body.name;
        // let password = req.body.password;
        // let sql = 'select * from user_table where name = \'' + name + '\' and' +
        //     ' password=' + password + ';';
        // let conn = 'user_db';

        // myUnit.myDB.sql.execute(conn, sql, function (err, results, fields) {
        //     if (err) {
        //         console.error(err);
        //     }
        //     if (results[0] === undefined) {
        //         res.status(404).send('mismatch');
        //         return 0;
        //     }
        /*
        * 改变了资源根目录后,需要重新设置路由
        * */
            // app.use(dirConfig.register_rootDir,express.static(path.resolve('../../TaskAndModule/dist_prod')));
            res.send(dirConfig.register_rootDir+'/file_upload.html');
        // });
    });

    upload_code_control(app,dirConfig.register_rootDir);
    upload_text_control(app,dirConfig.register_rootDir);
};
module.exports = file_upload_express;

